package com.socialin.picsin.camera.view;

import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;

public class ShapeDrawer {
	private final static int TOUCH_MOVE_TOLERANCE = 2;
	private int rectTop;
	private int rectLeft;
	private int rectWidth;
	private int rectHeight;
	private int oldX = -1;
	private int oldY = -1;
	
	private int maxX;
	private int maxY;
	
	public ShapeDrawer(int rectLeft,int rectTop,int rectWidth,int rectHeight,int maxX,int maxY)
	{
		this.rectLeft = rectLeft;
		this.rectTop = rectTop;
		this.rectWidth = rectWidth;
		this.rectHeight = rectHeight;
		this.maxX = maxX;
		this.maxY = maxY;
	}
	public void touchDown(int x,int y)
	{
		oldX = x;
		oldY = y;
	}
	
	public boolean touchMove(int x,int y)
	{
		int dx = x-oldX;
		int dy = y-oldY;
		//if(Math.abs(dx)>TOUCH_MOVE_TOLERANCE || Math.abs(dy)>TOUCH_MOVE_TOLERANCE)
		{
			Log.e("ex", ">> TOUCH_MOVE_TOLERANCE");
			int newRectLeft = rectLeft+dx;
			int newRectTop = rectTop+dy;
			Log.e("ex", "maxX = "+maxX);
			Log.e("ex", "maxY = "+maxY);
			if(newRectLeft>=0 && newRectTop>=0 && (newRectLeft+rectWidth)<=maxX && (newRectTop+rectHeight)<=maxY)
			{
				Log.e("ex", "not out of screen");
				rectLeft = newRectLeft;
				rectTop = newRectTop;
				oldX = x;
				oldY = y;
				return true;
			}
			oldX = x;
			oldY = y;
		}
		return false;
	}
	
	public boolean fullTouch(MotionEvent event)
	{
		int x = (int)event.getX();
		int y = (int)event.getY();
		//
		{
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				if(x>rectLeft && x<(rectLeft+rectWidth) && y>rectTop && y<(rectTop+rectHeight))
				touchDown(x, y);
				return false;

			case MotionEvent.ACTION_MOVE:
				if(oldX!=-1)
				{
					return touchMove(x, y);
				}
				break;
			case MotionEvent.ACTION_UP:
				oldX = -1;
				oldY = -1;
				break;
		}
		}
		return false;
	}
	
	public Rect getShapeRect()
	{
		return new Rect(rectLeft, rectTop, rectLeft+rectWidth, rectTop+rectHeight);
	}
	
	public void setScreenSize(int width,int height)
	{
		maxX = width;
		maxY = height;
	}
}
